package com.xiaoquan.gulimall.ware.service.impl;

import com.xiaoquan.common.utils.R;
import com.xiaoquan.gulimall.ware.feign.ProductFeignService;
import com.xiaoquan.gulimall.ware.vo.SkuHasStockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoquan.common.utils.PageUtils;
import com.xiaoquan.common.utils.Query;

import com.xiaoquan.gulimall.ware.dao.WareSkuDao;
import com.xiaoquan.gulimall.ware.entity.WareSkuEntity;
import com.xiaoquan.gulimall.ware.service.WareSkuService;
import org.springframework.transaction.annotation.Transactional;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    WareSkuDao wareSkuDao;

    @Autowired
    ProductFeignService productFeignService;

    /**
     * skuId
     * wareId
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();

        String skuId = (String) params.get("skuId");
        if (skuId != null && !skuId.trim().equals("")) {
            queryWrapper.eq("sku_id",skuId);
        }
        String wareId = (String) params.get("wareId");
        if (wareId != null && !wareId.trim().equals("")) {
            queryWrapper.eq("ware_id",wareId);
        }


        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Transactional  // 开启事务 一定要在主方法中 开始事务注解
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        // 1、判断如果还没有这个库存的记录 就是新增 否则就是修改
        List<WareSkuEntity> wareSkuEntities = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>()
                .eq("sku_id", skuId).eq("ware_id", wareId));
        if (wareSkuEntities == null || wareSkuEntities.size() == 0){// 没有就是 添加
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setStockLocked(0);
            try { // TODO 远程调用sku的名字 如果失败 无需回滚  1、try catch
                R info = productFeignService.info(skuId);
                Map<String, Object> data = (Map<String, Object>) info.get("skuInfo");
                if (info.getCode() == 0) { // 等于0 说明成功了
                    wareSkuEntity.setSkuName((String) data.get("skuName"));  // 这是一个冗余的字段 失败了也没有关系  不需要进行事务回滚
                }
            }catch (Exception e){
                log.error("远程服务ProductFeignService: 调用失败");
            }
            wareSkuDao.insert(wareSkuEntity);
        }else { // 有记录才是更新
            wareSkuDao.addStock(skuId,wareId,skuNum);
        }

    }

    @Override
    public List<SkuHasStockVo> getSkusHasStock(List<Long> skuIds) {
        List<SkuHasStockVo> collect = skuIds.stream().map(skuId -> {
            SkuHasStockVo skuHasStock = new SkuHasStockVo();
            // 查询当前sku的总库存量
            // SELECT SUM(stock-stock_locked) from `wms_ware_sku` where sku_id = #{id}
            Long count = this.baseMapper.getSkuStock(skuId);

            skuHasStock.setSkuId(skuId);
            skuHasStock.setHasStock(count==null ? false : count > 0);
            return skuHasStock;
        }).collect(Collectors.toList());

        return collect;
    }

}